package gov.nist.applet.phone.security.filetransfer;



import gov.nist.applet.phone.security.DESEncoder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Random;

import javax.swing.JOptionPane;

public class ReceiveFile
{
	private Socket socket = null;

	private String ip = "localhost"; // 设置成服务器IP
	
	private String savePath = null;

	private int port = 8077;
	//private int port = 6070;

	public ReceiveFile(String ip)
	{
		this.ip = ip;

	}

	/**
	 * create the socket connect to server
	 * @return true-if connect it success.
	 */
	private boolean createConnection()
	{
		try
		{
			socket = new Socket(ip,port);
			System.out.println("连接服务器成功! port:"+port+" ip:"+ip);
			return true;
		} catch (Exception e)
		{
			System.out.println("连接服务器失败!");
			return false;
		}

	}

	/**
	 * receive the data server send 
	 */
	public void receive()
	{

		try
		{
			boolean result = createConnection();
			if(result==false)
				return;
			// 本地保存路径，文件名会自动从服务器端继承而来。
			//String savePath = "E:/";
			// int bufferSize = 8192;
			int bufferSize = 1024*4;
			byte[] buf = new byte[bufferSize];
			int passedlen = 0;
			long len = 0;

			DataInputStream dis = new DataInputStream(
			new BufferedInputStream(socket.getInputStream()));
			// 从输入流中读取服务器为端发来的文件名和文件长度，这2个数据
			savePath += dis.readUTF();
			len = dis.readLong();

			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					new BufferedOutputStream(new FileOutputStream(savePath))));
			System.out.println("文件的长度为:" + len);
			System.out.println("开始接收文件!");
			
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					new BufferedInputStream(socket.getInputStream())));
			String temp ;
			while(true)
			{
				temp = bf.readLine();
				if(temp==null)
					break;
				buf = temp.getBytes("UTF-8");
				System.out.println(temp);
				
				// 对字节数组里面的数据进行解密
				DESEncoder desEncoder = new DESEncoder("123456");
			//	buf = desEncoder.encryptStr(new String(buf)).getBytes();
				temp = desEncoder.decryptStr(temp);
				System.out.println(temp);
				bw.write(temp);
				bw.newLine();
			}
			
			System.out.println("接收完成，文件存为" + savePath);
			JOptionPane.showMessageDialog(null,"接收完成，文件存为"+savePath,"消息",JOptionPane.INFORMATION_MESSAGE);
			bf.close();
			bw.close();
		} catch (Exception e)
		{
			System.out.println("接收消息错误");
			e.printStackTrace();
			return;
		}
	}
	
	/**
	 * get the path for saving a file
	 * @return
	 */
	public String getSavePath()
	{
		return savePath;
	}

	public void setSavePath(String path)
	{
		this.savePath = path;
	}
	public String getIp()
	{
		return ip;
	}

	public void setIp(String ip)
	{
		this.ip = ip;
	}

	public int getPort()
	{
		return port;
	}

	public void setPort(int port)
	{
		this.port = port;
	}

	/**
	 * just for a test
	 * @param arg
	 */
	public static void main(String arg[])
	{
		new ReceiveFile("localhost").receive();
		
	}
}